<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    include "../../include/dbFunctions.php";
    function deleteVolunteer($Id) {
        $id = $Id;
        $query_check = "SELECT volunteer_id,event_id  FROM trainer WHERE volunteer_id = $id";
        $result_check = executeSelectQuery($query_check);
        $check = count($result_check);
        if ($check!=0) {
            $mes = "This record cannot be deleted because it is found in some courses.";
        } else {
            $que = "SELECT volunteer_cv FROM volunteer WHERE volunteer_id = $id";
            $re = executeSelectQuery($que);
            $file="../../upload/cv/".$re[0]['volunteer_cv'];
            $query4 = "DELETE FROM education_preference WHERE volunteer_id = $id";
            $query2 = "DELETE FROM location_preference WHERE volunteer_id = $id";
            $query3 = "DELETE FROM time_preference WHERE volunteer_id = $id";
            $query1 = "DELETE FROM volunteer WHERE volunteer_id = $id";
            $result4 = executeQuery($query4);
            $result2 = executeQuery($query2);
            $result3 = executeQuery($query3);
            $result1 = executeQuery($query1);
            if ($re[0]['volunteer_cv']!=""){
                unlink($file);
            }
            if ($result1) {
                $mes = "Your record has been deleted successfully";
            } else {
                $mes = "Server Error! Please try again";
            }
        }
        return $mes;
    }

    if (isset($_POST['id'])) {
        $id_array=$_POST['id'];
        for($i=0;$i<count($id_array);$i++) {
            $id = $id_array[$i];
            $_SESSION['mes'] = deleteVolunteer($id);
        }
    }
    if (isset($_GET['id'])) {
        $id = $_GET['id'];
        $_SESSION['mes']=deleteVolunteer($id);
    }

    header('Location: ../volunteerManage.php');
    ?>
    <?php
}else {
    header("Location: ../include/login.html");
}
?>